import {
  clearAll
} from "@/utils/auth";
import store from '@/store'
import router from '@/router'
import {
  setToken,
  setUserMsg
} from "@/utils/auth";
import {
  logout,
  login
} from "@/api/user.js";
const auth = {
  state: {
    token: null,
    userMsg: {}
  },
  getters: {
    token: state => state.token,
    userMsg: state => state.userMsg,
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token // 设置token
    },
    SET_USER_MSG: (state, userMsg) => {
      state.userMsg = userMsg // 设置用户信息
    },
  },
  actions: {
    setToken({
      commit
    }, token) {
      commit("SET_TOKEN", token)
    },
    setUserMsg({
      commit
    }, userMsg) {
      commit("SET_USER_MSG", userMsg)
    },
    LogOut({
        commit
      },
      param) {
      return new Promise((resolve, reject) => {
        // logout(param).then(response => {
        commit("SET_TOKEN", "");
        commit("SET_USER_MSG", {});
        clearAll();
        resolve()
        // })
      })
    },
    login({
      commit
    }, param) {
      return new Promise((resolve, reject) => {
        // login(param).then(response => {
        const token = "admin-token";
        const userMsg = {};
        commit("SET_TOKEN", token);
        commit("SET_USER_MSG", userMsg);
        setToken(token);
        setUserMsg(userMsg);
        resolve()
        // })
      })
    },
  }
}
export default auth
